﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DiveManager.Benutzerverwaltung;

namespace DiveManager
{
    public partial class LoginForm : Form
    {
        protected DiveDatabaseEntities diveDatabase = new DiveDatabaseEntities();

        // Properties
        public bool CorrectPass { get; set; }

        public LoginForm()
        {
            InitializeComponent();

            CorrectPass = false;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string username = tbUsername.Text;
            string pass = new Security().GetMD5Hash(tbPassword.Text);
            checkUsernamePassword(username, pass);
        }

        private void btnDiveStar_Click(object sender, EventArgs e)
        {
            string username = "DiveStar";
            string pass = new Security().GetMD5Hash("DiveStar");
            checkUsernamePassword(username, pass );
        }

        private void checkUsernamePassword(string username, string pass)
        {
            IQueryable<User> userset = (from   a in diveDatabase.User
                                        where  a.Name == username && a.Password == pass
                                        select a);
            if (userset.Count() == 0)
            {
                lbMessage.Visible = true;
            }
            else
            {
                this.TopMost = false;
                Properties.Settings.Default.username = username;
                Properties.Settings.Default.Save();                
                CorrectPass = true;

                username = "";
                username = Properties.Settings.Default.username;

                //Passwort ist initial? Dann doch bitte ändern!
                User loginUsr = userset.First();
                if (loginUsr.initialpassword == true)
                {
                    PasswordResetForm prf = new PasswordResetForm();
                    if (prf.ShowDialog() == System.Windows.Forms.DialogResult.OK && prf.NewPass != null)
                    {
                        Guid usrID = loginUsr.ID;
                        if (usrID != null)
                        {
                            User usr = (from a in diveDatabase.User
                                        where a.ID == usrID
                                        select a).First();
                            usr.Password = new Security().GetMD5Hash(prf.NewPass);
                            usr.initialpassword = false;
                            diveDatabase.SaveChanges();
                            MessageBox.Show("Passwort wurde geändert!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        Close();
                    }
                    else
                    {
                        Environment.Exit(0);
                    }
                }
                else
                {
                    Close();
                }
            }            
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {

        }
    }
}
